<?php
/**
 * +----------------------------------------------------------------------
 * | 管理员登录日志模型
 * +----------------------------------------------------------------------
 */
namespace app\model;

// 引入框架内置类
use think\Model;

class SysLoginNote extends Model
{
    protected $pk = 'id';

    protected $schema = [
        "id" => "int",  // id
        "userid" => "int",  // 用户编号
        "username" => "varchar",  // 用户名
        "logintime" => "datetime",  // 登录时间
        "loginip" => "varchar",  // 登录ip
        "status" => "tinyint",  // 登录状态,0 成功; 1 密码错误; 2 禁用用户
    ];

    protected $autoWriteTimestamp = false; 

    // 管理员登录日志记录
    public static function record(int $userid, string $username, string $logintime, string $loginip, int $status = 0)
    {
        // 入库
        self::create([
            'userid'    => $userid,
            'username'  => $username,
            'logintime' => $logintime,
            'loginip'   => $loginip,
            'status'    => $status,
        ]);
    }

    public static function queryPage($limit = 30, $userid = 0)
    {
        if ($userid==0) {
            $list = self::order("id desc")->paginate($limit);
        } else {
            $list = self::where('userid', $userid)->order("id desc")->paginate($limit);
        }
        
        return $list;
    }
}
